package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.AgreementControl;
import org.hgvc.webapp.ets.bo.CommissionPaymentSchema;
import org.hgvc.webapp.ets.bo.CommissionSchema;
import org.hgvc.webapp.ets.constant.AgentConstant;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminAgentRegApprBean extends AbstractBean {

	public static final String BEAN_NAME="adminAgentRegApprBean";
	public static final String AB_INVOKE_ACTION_METHOD="agentApprovalInvokeAction";
	public static final String NAV_AGENT_APPROVAL="adminAgentRegAppr";
	public static final String NAV_AGENT_ASSIGN_AGGR="adminAgentAssignAgrr";
	private String code;
	private String reason;
	private String actionNo;
	private AgentService service;
	private Agent agent;
	private List<SelectItem> commPaySchList;
	private List<SelectItem> commSchList;
	private String commPaySchId;
	private String commSchId;
	private AgreementControl control;
	private boolean flag = false;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	public String getActionNo() {
		return actionNo;
	}
	public void setActionNo(String actionNo) {
		this.actionNo = actionNo;
	}
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public void setService(AgentService service) {
		this.service = service;
	}
	public String getCommPaySchId() {
		return commPaySchId;
	}
	public void setCommPaySchId(String commPaySchId) {
		this.commPaySchId = commPaySchId;
	}
	public String getCommSchId() {
		return commSchId;
	}
	public void setCommSchId(String commSchId) {
		this.commSchId = commSchId;
	}
	public AgreementControl getControl() {
		return control;
	}
	public void setControl(AgreementControl control) {
		this.control = control;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	/*-------------------------------------------------------------------------*/
	public String agentApprovalInvokeAction(String actionNo,String... params){
		String id = params[1];
		try{
			code = null;
			reason = null;
			this.actionNo = actionNo;
			agent = service.getAgentById(id);
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_AGENT_APPROVAL.concat("_"+getRoleName());
	}
	public List<SelectItem> getCommPaySchList() {
		try{
			commPaySchList = new ArrayList<SelectItem>();
			List<CommissionPaymentSchema> list = service.getCommissionPaymentSchemaListType(AgentConstant.AGENT_COMM_PAY_SCH_ADMIN_TYPE);
			for(CommissionPaymentSchema sc:list){
				SelectItem item = new SelectItem(sc.getCommPaymentSchId(),sc.getCode()+" "+sc.getName());
				commPaySchList.add(item);
			}
			
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return commPaySchList;
	}
	public List<SelectItem> getCommSchList() {
		try{
			commSchList = new ArrayList<SelectItem>();
			List<CommissionSchema> list = service.getCommissionSchemaListType(AgentConstant.AGENT_COMM_SCH_ADMIN_TYPE);
			for(CommissionSchema sc:list){
				SelectItem item = new SelectItem(sc.getCommSchId(),sc.getCode()+" "+sc.getName());
				commSchList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return commSchList;
	}
	
	public String navCreateAction(){
		control = new AgreementControl();
		return NAV_AGENT_ASSIGN_AGGR.concat("_"+getRoleName());
	}
	
	public String createAction(){
		flag = true;
		return NAV_AGENT_APPROVAL.concat("_"+getRoleName());
	}
	
	
	public String navAgentAssignAggr(){
		return NAV_AGENT_ASSIGN_AGGR.concat("_"+getRoleName());
	}
	
	public String navAgentAppr(){
		return NAV_AGENT_APPROVAL.concat("_"+getRoleName());
	}
	
	public String approvaAction(){
		try{
			if(code==null||code.length()==0){
				throw new BaseException(SpringUtils.getMessage("error.agent.code.required", new Object[]{}));
			}
		/*	if(!flag){
				throw new BaseException(SpringUtils.getMessage("error.please.assign.agent.agreement.no", new Object[]{}));
			}*/
			agent.setSchema(new CommissionSchema(commSchId));
			agent.setPaymentSchema(new CommissionPaymentSchema(this.commPaySchId));
			service.approveAgentRegistration(agent, control, getThisUser(), reason, code, actionNo);
			return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_AGENT_APPROVAL.concat("_"+getRoleName());
	}
	
	public String rejectAction(){
		try{
			service.rejectAgentRegistration(agent, getThisUser(), reason, actionNo);
			return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_AGENT_APPROVAL.concat("_"+getRoleName());
	}
	
	public String navPortal(){
		return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
	}
}
